<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\User;
use app\common\tool\Result;
use think\App;
use think\facade\View;
use think\Response;

class Relation extends Base
{
    public function index(): string
    {
        $keyword = $this->request->get('keyword');
        if ($keyword) {
            $map['code|nickname|mobile'] = $keyword;
        } else {
            $map['parent_id'] = 0;
        }
        $list = User::field('id,code,nickname,mobile,level_id')->where($map)
            ->order('is_active desc,active_time asc')->select();
        foreach ($list as &$v) {
            $level = $v->level ? $v->level->title : '无';
            $v->title = '编号【' . $v['code'] . '】
                姓名【' . $v->nickname . '】
                手机号【' . $v->mobile . '】
                等级【' . $level . '】
              ';
        }
        View::assign('list', $list);

        return View::fetch();
    }

    public function getData(): Response
    {
        $id = $this->request->get('id');
        $list = User::field('id,code,nickname,mobile,level_id')->where(['parent_id' => $id])
            ->order('is_active desc,active_time asc')->select();
        foreach ($list as &$v) {
            $level = $v->level ? $v->level->title : '无';
            $v->title = '编号【' . $v['code'] . '】
                姓名【' . $v->nickname . '】
                手机号【' . $v->mobile . '】
                等级【' . $level . '】
              ';
        }

        return Result::success('success', $list);
    }
}